import 'package:equatable/equatable.dart';

class LivestreamModel extends Equatable {
  final int id;
  final String title;
  final String description;
  final int topicId;
  final TopicModel topic;
  final int streamerId;
  final UserModel streamer;
  final String rtmpUrl;
  final String hlsUrl;
  final String status;
  final int viewerCount;
  final int maxViewers;
  final DateTime? startTime;
  final DateTime? endTime;
  final int duration;
  final String? thumbnail;
  final String? recordingUrl;
  final bool isRecording;
  final List<String> tags;
  final DateTime createdAt;
  final DateTime updatedAt;

  const LivestreamModel({
    required this.id,
    required this.title,
    required this.description,
    required this.topicId,
    required this.topic,
    required this.streamerId,
    required this.streamer,
    required this.rtmpUrl,
    required this.hlsUrl,
    required this.status,
    required this.viewerCount,
    required this.maxViewers,
    this.startTime,
    this.endTime,
    required this.duration,
    this.thumbnail,
    this.recordingUrl,
    required this.isRecording,
    required this.tags,
    required this.createdAt,
    required this.updatedAt,
  });

  factory LivestreamModel.fromJson(Map<String, dynamic> json) {
    return LivestreamModel(
      id: json['id'],
      title: json['title'],
      description: json['description'],
      topicId: json['topic_id'],
      topic: TopicModel.fromJson(json['topic']),
      streamerId: json['streamer_id'],
      streamer: UserModel.fromJson(json['streamer']),
      rtmpUrl: json['rtmp_url'],
      hlsUrl: json['hls_url'],
      status: json['status'] ?? 'pending',
      viewerCount: json['viewer_count'] ?? 0,
      maxViewers: json['max_viewers'] ?? 1000,
      startTime: json['start_time'] != null ? DateTime.parse(json['start_time']) : null,
      endTime: json['end_time'] != null ? DateTime.parse(json['end_time']) : null,
      duration: json['duration'] ?? 0,
      thumbnail: json['thumbnail'],
      recordingUrl: json['recording_url'],
      isRecording: json['is_recording'] ?? false,
      tags: List<String>.from(json['tags'] ?? []),
      createdAt: DateTime.parse(json['created_at']),
      updatedAt: DateTime.parse(json['updated_at']),
    );
  }

  Map<String, dynamic> toJson() {
    return {
      'id': id,
      'title': title,
      'description': description,
      'topic_id': topicId,
      'topic': topic.toJson(),
      'streamer_id': streamerId,
      'streamer': streamer.toJson(),
      'rtmp_url': rtmpUrl,
      'hls_url': hlsUrl,
      'status': status,
      'viewer_count': viewerCount,
      'max_viewers': maxViewers,
      'start_time': startTime?.toIso8601String(),
      'end_time': endTime?.toIso8601String(),
      'duration': duration,
      'thumbnail': thumbnail,
      'recording_url': recordingUrl,
      'is_recording': isRecording,
      'tags': tags,
      'created_at': createdAt.toIso8601String(),
      'updated_at': updatedAt.toIso8601String(),
    };
  }

  LivestreamModel copyWith({
    int? id,
    String? title,
    String? description,
    int? topicId,
    TopicModel? topic,
    int? streamerId,
    UserModel? streamer,
    String? rtmpUrl,
    String? hlsUrl,
    String? status,
    int? viewerCount,
    int? maxViewers,
    DateTime? startTime,
    DateTime? endTime,
    int? duration,
    String? thumbnail,
    String? recordingUrl,
    bool? isRecording,
    List<String>? tags,
    DateTime? createdAt,
    DateTime? updatedAt,
  }) {
    return LivestreamModel(
      id: id ?? this.id,
      title: title ?? this.title,
      description: description ?? this.description,
      topicId: topicId ?? this.topicId,
      topic: topic ?? this.topic,
      streamerId: streamerId ?? this.streamerId,
      streamer: streamer ?? this.streamer,
      rtmpUrl: rtmpUrl ?? this.rtmpUrl,
      hlsUrl: hlsUrl ?? this.hlsUrl,
      status: status ?? this.status,
      viewerCount: viewerCount ?? this.viewerCount,
      maxViewers: maxViewers ?? this.maxViewers,
      startTime: startTime ?? this.startTime,
      endTime: endTime ?? this.endTime,
      duration: duration ?? this.duration,
      thumbnail: thumbnail ?? this.thumbnail,
      recordingUrl: recordingUrl ?? this.recordingUrl,
      isRecording: isRecording ?? this.isRecording,
      tags: tags ?? this.tags,
      createdAt: createdAt ?? this.createdAt,
      updatedAt: updatedAt ?? this.updatedAt,
    );
  }

  bool get isLive => status == 'live';
  bool get isEnded => status == 'ended';
  bool get isPending => status == 'pending';

  @override
  List<Object?> get props => [
        id,
        title,
        description,
        topicId,
        topic,
        streamerId,
        streamer,
        rtmpUrl,
        hlsUrl,
        status,
        viewerCount,
        maxViewers,
        startTime,
        endTime,
        duration,
        thumbnail,
        recordingUrl,
        isRecording,
        tags,
        createdAt,
        updatedAt,
      ];
}

// Import TopicModel and UserModel if not already imported
import 'topic_model.dart';
import 'user_model.dart';